<?php
namespace app\common\service\GoodsService;

use app\Normal;
use app\model\BbcModel\HotWords;

class HotWordsService
{
    /**
     * 获取列表分页
     * @param type $param
     * @return array
     */
    public function getListByPage( $param):array {
        $page=(isset($param['page'])&&$param['page']>0)?$param['page']:1;
        $limit=(isset($param['limit'])&&$param['limit']>0)?$param['limit']:20;
        $where=[
            ['df','=',0],
            ['cid','=',$param['cid']],
        ];
        if(isset($param['search']) && $param['search'] !==''){
            $where[] = ['words', 'like', '%'.$param['search'].'%'];
        }
        $data= HotWords::instance()->listPages($where, $limit, $page);
        $count= HotWords::instance()->countNum($where);
        foreach ($data as &$item) {
            $item['cd']=$item['cd']?date('Y-m-d H:i:s',$item['cd']):'';
            $item['md']=$item['md']?date('Y-m-d H:i:s',$item['md']):'';
        }
        return Normal::returnCode(Normal::SUC,"获取成功",['list'=>$data,'total'=>$count]);
    }
    /**
     * 新增数据
     * @param array $param
     * @return array
     */
    public function addWords(array $param):array {
        $words = trim($param['words']) ?? '';
        if (empty($words)) {
            return Normal::returnCode(Normal::FAIL,"热词不能为空");
        }
        $repwhere=[['words','=',$param['words']],['cid','=',$param['cid']]];
        $rep= HotWords::instance()->info($repwhere);
        if($rep){
            return Normal::returnCode(Normal::FAIL,"该词语重复");
        }
        $add_data = [
            'words' => $words,
            'cd' => time(),
            'cid' => $param['cid'],
            'cb_id'=> $param['cb_id']??0
        ];
        $res = HotWords::instance()->add($add_data);
        if($res){
            return Normal::returnCode(Normal::SUC,"新增成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"新增失败");
        }
    }
    
    /**
     * 修改消息
     * @param array $param
     * @return array
     */
    public function editWords(array $param):array {
        $words = trim($param['words']) ?? '';
        if (empty($words)) {
            return Normal::returnCode(Normal::FAIL,"热词不能为空");
        }
        $hotwords= HotWords::instance()->info(['id'=>$param['id']]);
        if(empty($hotwords)){
            return Normal::returnCode(Normal::FAIL,"未找到相关信息");
        }
        $repwhere=[['words','=',$param['words']],['cid','=',$param['cid']],['id','<>',$param['id']]];
        $rep= HotWords::instance()->info($repwhere);
        if($rep){
            return Normal::returnCode(Normal::FAIL,"该词语重复");
        }
        $edit_data = [
            'words'=>$words,
            'md' => time(),
        ];
        $res = HotWords::instance()->edit(['id'=>$param['id']], $edit_data);
        if($res){
            return Normal::returnCode(Normal::SUC,"修改成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"修改失败");
        }
    }
    
    /**
     * 删除消息
     * @param array $param
     * @return array
     */
    public function delWords(array $param):array {
        if (empty($param['id'])) {
            return Normal::returnCode(Normal::FAIL,"id必传");
        }
        $hotword= HotWords::instance()->info(['id'=>$param['id']]);
        if(empty($hotword)){
            return Normal::returnCode(Normal::FAIL,"未找到相关信息");
        }
        $res = HotWords::instance()->del(['id'=>$param['id']]);
        if($res){
            return Normal::returnCode(Normal::SUC,"删除成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"删除失败");
        }
    }
}
